package com.xdja.jwt.imagecollect.app.adapter

import android.os.Bundle
import android.widget.ImageView
import android.widget.ProgressBar
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.Utils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.jess.arms.http.imageloader.glide.ImageConfigImpl
import com.jess.arms.utils.ArmsUtils
import com.xdja.jwt.imagecollect.R
import com.xdja.jwt.imagecollect.app.db.entity.UploadFileBean
import com.xdja.jwt.imagecollect.app.ext.isImage
import com.xdja.jwt.imagecollect.app.ext.isVideo
import com.xdja.jwt.imagecollect.app.utils.DiffUploadCallBack
import com.xdja.jwt.imagecollect.app.utils.OpenFileUtil

class UploadingAdapter(data: MutableList<UploadFileBean>?) :
    BaseQuickAdapter<UploadFileBean, BaseViewHolder>(R.layout.item_uploading_list, data) {

    override fun convert(helper: BaseViewHolder, item: UploadFileBean) {
        helper.setText(R.id.tv_video_name, item.filename)
        val imageView = helper.getView<ImageView>(R.id.img_video_cover)
        if (item.filePath.isNotEmpty()) {
            val fileType = item.filename.substringAfterLast(".")
            when {
                item.filePath.isImage() -> {
                    ArmsUtils.obtainAppComponentFromContext(Utils.getApp())
                        .imageLoader()
                        .loadImage(
                            Utils.getApp(), ImageConfigImpl
                                .builder()
                                .placeholder(R.drawable.pic_small)
                                .url(item.filePath)
                                .imageView(imageView)
                                .build()
                        )
                }
                item.filePath.isVideo() -> {
                    ArmsUtils.obtainAppComponentFromContext(Utils.getApp())
                        .imageLoader()
                        .loadImage(
                            Utils.getApp(), ImageConfigImpl
                                .builder()
                                .placeholder(R.drawable.video)
                                .url(item.filePath)
                                .imageView(imageView)
                                .build()
                        )
                }
                OpenFileUtil.voice.contains(fileType) -> imageView.setImageResource(R.drawable.voice_small)
                OpenFileUtil.apk.contains(fileType) -> imageView.setImageResource(R.drawable.apk_small)
                OpenFileUtil.ppt.contains(fileType) -> imageView.setImageResource(R.drawable.ppt_small)
                OpenFileUtil.xls.contains(fileType) -> imageView.setImageResource(R.drawable.excel_small)
                OpenFileUtil.doc.contains(fileType) -> imageView.setImageResource(R.drawable.word_small)
                OpenFileUtil.pdf.contains(fileType) -> imageView.setImageResource(R.drawable.pdf_small)
                OpenFileUtil.chm.contains(fileType) -> imageView.setImageResource(R.drawable.chm_small)
                OpenFileUtil.txt.contains(fileType) -> imageView.setImageResource(R.drawable.txt_small)
                OpenFileUtil.rar.contains(fileType) -> imageView.setImageResource(R.drawable.rar_small)
                OpenFileUtil.zip.contains(fileType) -> imageView.setImageResource(R.drawable.zip_small)
                else -> imageView.setImageResource(R.drawable.unknown_small)
            }
        }
        if (item.totalSize != 0L) {
            val progress = item.uploadSize * 100 / item.totalSize
            helper.setText(R.id.tv_progress, "${progress}%")
            helper.getView<ProgressBar>(R.id.progressBar).progress = progress.toInt()
        }
        helper.setText(R.id.tv_folder_name, item.folderName)
        if (item.hasStart) {
            helper.setGone(R.id.iv_state, false)
            helper.setGone(R.id.tv_progress, true)
            helper.setText(R.id.tv_state, "上传中")
            helper.setTextColor(R.id.tv_state, ContextCompat.getColor(mContext, R.color.colorGray))
        } else {
            helper.setGone(R.id.iv_state, true)
            helper.setGone(R.id.tv_progress, false)
            helper.setImageResource(R.id.iv_state, R.drawable.small_pause)
            helper.setText(R.id.tv_state, "点击继续")
            helper.setTextColor(R.id.tv_state, ContextCompat.getColor(mContext, R.color.colorGray))
        }
        if (item.hasError) {
            helper.setGone(R.id.iv_state, true)
            helper.setGone(R.id.tv_progress, false)
            helper.setImageResource(R.id.iv_state, R.drawable.warning)
            helper.setText(R.id.tv_state, "点击重试")
            helper.setTextColor(
                R.id.tv_state,
                ContextCompat.getColor(mContext, R.color.color_warning)
            )
            helper.getView<ProgressBar>(R.id.progressBar).progressDrawable =
                ContextCompat.getDrawable(mContext, R.drawable.bg_uploading_error_progress)
        } else {
            helper.getView<ProgressBar>(R.id.progressBar).progressDrawable =
                ContextCompat.getDrawable(mContext, R.drawable.bg_uploading_progress)
        }
        helper.addOnClickListener(R.id.tvState)
    }

    override fun convertPayloads(
        helper: BaseViewHolder,
        item: UploadFileBean,
        payloads: MutableList<Any>
    ) {
        val bundle = payloads[0] as Bundle
        for (key in bundle.keySet()) {
            if (key == DiffUploadCallBack.hasStart) {
                val boolean = bundle.getBoolean(DiffUploadCallBack.hasStart)
                if (boolean) {
                    helper.setGone(R.id.iv_state, false)
                    helper.setGone(R.id.tv_progress, true)
                    helper.setText(R.id.tv_state, "上传中")
                    helper.setTextColor(
                        R.id.tv_state,
                        ContextCompat.getColor(mContext, R.color.colorGray)
                    )
                } else {
                    helper.setGone(R.id.iv_state, true)
                    helper.setGone(R.id.tv_progress, false)
                    helper.setImageResource(R.id.iv_state, R.drawable.small_pause)
                    helper.setText(R.id.tv_state, "点击继续")
                    helper.setTextColor(
                        R.id.tv_state,
                        ContextCompat.getColor(mContext, R.color.colorGray)
                    )
                }
            }
            if (key == DiffUploadCallBack.hasError) {
                val boolean = bundle.getBoolean(DiffUploadCallBack.hasError)
                if (boolean) {
                    helper.setGone(R.id.iv_state, true)
                    helper.setGone(R.id.tv_progress, false)
                    helper.setImageResource(R.id.iv_state, R.drawable.warning)
                    helper.setText(R.id.tv_state, "点击重试")
                    helper.setTextColor(
                        R.id.tv_state,
                        ContextCompat.getColor(mContext, R.color.color_warning)
                    )
                    helper.getView<ProgressBar>(R.id.progressBar).progressDrawable =
                        ContextCompat.getDrawable(mContext, R.drawable.bg_uploading_error_progress)
                } else {
                    helper.getView<ProgressBar>(R.id.progressBar).progressDrawable =
                        ContextCompat.getDrawable(mContext, R.drawable.bg_uploading_progress)
                }
            }
            if (key == DiffUploadCallBack.uploadSize) {
                val size = bundle.getLong(DiffUploadCallBack.uploadSize)
                if (item.totalSize != 0L) {
                    val progress = size * 100 / item.totalSize
                    helper.setText(R.id.tv_progress, "${progress}%")
                    helper.getView<ProgressBar>(R.id.progressBar).progress = progress.toInt()
                }
            }
        }
    }
}